<template>
  <div class="user-info">
    <!-- 个人信息 -->
    <el-form ref="userForm" label-width="220px" :model="userInfo">
      <!-- 工号 入职时间 -->
      <el-row class="inline-info">
        <el-col :span="12">
          <el-form-item label="工号">
            <el-input v-model="userInfo.workNumber" class="inputW" />
          </el-form-item>
        </el-col>

        <el-col :span="12">
          <el-form-item label="入职时间">
            <el-date-picker
              v-model="userInfo.timeOfEntry"
              style="width: 300px"
              type="date"
              class="inputW"
            />
          </el-form-item>
        </el-col>
      </el-row>
      <!-- 姓名 部门 -->
      <el-row class="inline-info">
        <el-col :span="12">
          <el-form-item label="姓名">
            <el-input v-model="userInfo.username" class="inputW" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="部门">
            <el-input v-model="userInfo.departmentName" class="inputW" />
          </el-form-item>
        </el-col>
      </el-row>
      <!--手机 聘用形式  -->
      <el-row class="inline-info">
        <el-col :span="12">
          <el-form-item label="手机">
            <el-input v-model="userInfo.mobile" style="width: 300px" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="聘用形式">
            <el-select v-model="userInfo.formOfEmployment" class="inputW">
              <el-option
                v-for="item in EmployeeEnum.hireType"
                :key="item.id"
                :label="item.value"
                :value="item.id"
              />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <!-- 员工照片 -->
      <el-row class="inline-info">
        <el-col :span="12">
          <el-form-item label="员工头像">
            <!-- 放置上传图片 -->
            <UploadImg ref="imageUrl" />
          </el-form-item>
        </el-col>
      </el-row>
      <!-- 保存个人信息 -->
      <el-row class="inline-info" type="flex" justify="center">
        <el-col :span="12">
          <el-button type="primary" @click="saveUser">保存更新</el-button>
          <el-button @click="$router.back()">返回</el-button>

        </el-col>
      </el-row>
    </el-form>
  </div>
</template>

<script>
import EmployeeEnum from '@/constant/employees'
import { getUserDetailById, saveUserDetailById } from '@/api/user'
export default {
  name: 'UserInfo',
  data() {
    return {
      userInfo: {},
      EmployeeEnum

    }
  },
  created() {
    this.loadUserDetailById()
  },
  methods: {
    // 根据id获取用户信息
    async loadUserDetailById() {
      const res = await getUserDetailById(this.$route.query.id)
      // console.log(res)
      this.userInfo = res.data
      // 将获取到图片地址赋值给子组件的图片地址
      this.$refs.imageUrl.imageUrl = res.data.staffPhoto
    },
    // 更新个人详情
    saveUser() {
      // 获取子组件中的图片地址,存储到用户信息中
      this.userInfo.staffPhoto = this.$refs.imageUrl.imageUrl
      this.$refs.userForm.validate(async valid => {
        if (!valid) return
        const res = await saveUserDetailById(this.userInfo)
        // console.log(res)
        this.$message.success(res.message)
        this.$router.push('/employees')
      })
    }
  }
}
</script>

<style lang="scss" scoped></style>
